<?php

namespace App\Transformers\Admin;

use League\Fractal\TransformerAbstract;

class SectionTransformer extends TransformerAbstract
{
    public $availableIncludes = ['users'];

    public function transform($section)
    {
        return [
            'id' => $section->id,
            'name' => $section->name,
            'pid' => $section->pid,
            'level' => $section->level
        ];
    }

    /**
     * 额外的用户数据
     *
     * @param  $section
     * @return \League\Fractal\Resource\Collection
     */
    public function includeUsers($section)
    {
        return $this->collection($section->users, new class extends TransformerAbstract {
            public function transform($user) {
                return [
                    'id' => $user->id ?? null,
                    'username' => $user->username ?? null
                ];
            }
        });
    }
}

